import { Catch, MidwayHttpError } from '@midwayjs/core';
import { Context } from '@midwayjs/koa';
import { MidwayValidationError } from '@midwayjs/validate';
// step1 自定义一种异常类型
export class MissTargetError extends MidwayHttpError {
  constructor(msg) {
    super(msg, 999);
  }
}
// step2 捕获该异常并转化为http响应
@Catch(MissTargetError)
export class MissTargetErrorFilter {
  async catch(err: MissTargetError, ctx: Context) {
    ctx.status = err.status;
    return err.message;
  }
}
// step3 启动捕获器

@Catch(MidwayValidationError)
export class MidwayValidationErrorFilter {
  async catch(err: MidwayValidationError, ctx: Context) {
    console.error(err);
    ctx.status = 844;
    return err.message;
  }
}
